package com.moon.interview.common;

import cn.hutool.core.util.StrUtil;
import com.moon.interview.constant.CommonConstant;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

@Data
public class RequestParam {
    /** 当前页 */
    @Schema(name = "pageNum", description = "当前页")
    private int pageNum;

    /** 查询条数 */
    @Schema(name = "pageSize", description = "查询条数")
    private int pageSize;

    /**
     * 排序字段
     */
    private String sortField;

    /**
     * 排序顺序（默认升序）
     */
    private String sortOrder = CommonConstant.SORT_ORDER_ASC;

    public String getOrderSql() {
        if (StringUtils.isBlank(sortField)) {
            return "";
        }
        // 判断传入的sortOrder  默认升序
        sortOrder = CommonConstant.SORT_ORDER_ASC.equals(sortOrder) ? CommonConstant.SORT_ORDER_ASC : CommonConstant.SORT_ORDER_DESC;
        // 排序字段转数据库字段名_
        String underlineSortField = StrUtil.toUnderlineCase(sortField);
        return " order by " + underlineSortField+ " "+ sortOrder;
    }
}
